//
//  BattleLayer.m
//  ShoutBoss
//
//  Created by Luck Mstone on 6/15/09.
//  Copyright 2009 MS. All rights reserved.
//

#import "BattleLayer.h"
#import "Boss.h"
#import "Me.h"
#import "Shriek.h"
#import "Score.h"
#import "TerrainLayer.h"
#import "cocos2d.h"
#import "MainMenuScene.h"
#import "Constants.h"
#import "Goods.h"
#import "Good.h"


@implementation BattleLayer

-(id) init
{
	self = [super init];
	if(self != nil){	
		// 启动触摸功能
		isTouchEnabled = YES;
		
		// 初始化工作
		// 添加老板人物
		Boss *boss = [Boss node];
		boss.position = ccp(240, 140);
		[self addChild:boss z:0 tag:kTagBoss];
		// 向玩家移动
		[boss runAction: [MoveTo actionWithDuration:(450 - [boss position].x)/5 position:ccp(450, [boss position].y)]];
		
		// 添加自己人物
		Me *me = [Me node];
		[self addChild:me z:0 tag:kTagMe];
		
		// 添加尖叫声对象
		Shriek *shriek = [Shriek node];
		[self addChild:shriek z:0 tag:kTagShriek];
		
		// 添加分数对象
		Score *score = [Score node];
		[self addChild:score z:0 tag:kTagScore];
		
		// 地形层
		TerrainLayer *terrainlayer = [TerrainLayer node];
		[self addChild:terrainlayer z:0 tag:kTagTerrainlayer];
		
		[self schedule:@selector(checkbump)];
	}
	
	return self;
}

- (void) checkbump
{
	CocosNode *s = [self getChildByTag:kTagBoss];
	Sprite *sc = (Sprite *)[s getChildByTag:kTagBossBody];
	CGPoint tempposition = ccpAdd([s position], sc.position);
	CocosNode *t = [self getChildByTag:kTagTerrainlayer];
	Goods *g = (Goods*)[t getChildByTag:kTagGoods];
	for (int i = 0 ; i < g.goods.count ; i++ ) {
		Good *tc = (Good *)[g.goods objectAtIndex:i];
		CGPoint temppositiont = ccpAdd([t position], tc.position);
		
		// 检测与道具的碰撞
		if((temppositiont.x > tempposition.x) && (temppositiont.x < tempposition.x + tc.contentSize.width))
		{
			// 道具效果
			//[s stopAllActions];
			
			int step = (tc.type >= 6? 1: -1) * (6 - tc.type % 6) * 20;

			id jumpback = [Sequence actions:
						   [JumpBy actionWithDuration:0.5 position:ccp(step, 0) height:30 jumps:1],
						   [CallFunc actionWithTarget:self selector:@selector(bossmoving)],
						   nil];
			[s runAction: jumpback];
			[g removeChild:tc cleanup:YES];
			[g.goods removeObjectAtIndex:i];
		}
	}

	
	// BOSS位移到ME处，ME被打败
	if([s position].x >= 450)
	{
		// 高兴
		[s runAction:[Sequence actions:[MoveBy actionWithDuration:1 position:ccp(0, 100)],
					  [MoveBy actionWithDuration:1 position:ccp(0, -100)],
					  [CallFunc actionWithTarget:self selector:@selector(gameover)],
					  nil]];
		
		// 喷血效果
		ParticleSystem *blood = [ParticleExplosion node];
		blood.position = ccpAdd([s position], [s getChildByTag:kTagBossBody].position);
		[self addChild: blood z:0 tag:kTagBlood];
		blood.texture = [[TextureMgr sharedTextureMgr] addImage: @"stars.png"];
		
		// 关闭触摸功能
		[[Director sharedDirector] removeEventHandler:self];
	}
}

- (void) bossmoving
{
	CocosNode *s = [self getChildByTag:kTagBoss];
	[s stopAllActions];
	if([s position].x <= 150)
	{
		// 掉入陷阱
		[s runAction:[Sequence actions:[MoveBy actionWithDuration:1 position:ccp(0, -20)],
					  [RotateTo  actionWithDuration:2 angle:-90],
					  [CallFunc actionWithTarget:self selector:@selector(gamesuccess)],
					  nil]];

		// 喷血效果
		ParticleSystem *blood = [ParticleExplosion node];
		blood.position = ccpAdd([s position], [s getChildByTag:kTagBossBody].position);
		[self addChild: blood z:0 tag:kTagBlood];
		blood.texture = [[TextureMgr sharedTextureMgr] addImage: @"stars.png"];
		
		// 关闭触摸功能
		[[Director sharedDirector] removeEventHandler:self];

	}
	else
	{
		// 继续向玩家移动
		[s runAction: [MoveTo actionWithDuration:(450 - [s position].x)/5 position:ccp(450, [s position].y)]];
	}
}

- (void) gamesuccess
{
	// 切换到MainMenu
	MainMenuScene* mainMenuScene = [MainMenuScene node];
	[[Director sharedDirector] replaceScene:mainMenuScene];
}
	
- (void) gameover
{
	// 切换到MainMenu
	MainMenuScene* mainMenuScene = [MainMenuScene node];
	[[Director sharedDirector] replaceScene:mainMenuScene];
}

- (void) dealloc
{
	[super dealloc];
}

- (BOOL)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
	// 模拟吼叫效果
	CocosNode *s = [self getChildByTag:kTagBoss];
	[s stopAllActions];
	id moveback = [Sequence actions:
			   [MoveTo actionWithDuration:0.5 position:ccp([s position].x - rand()%30, [s position].y)],
			   [CallFunc actionWithTarget:self selector:@selector(bossmoving)],
				   nil];
	[s runAction: moveback];
	
	return kEventHandled;
}
@end
